package artid.com.arttogo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.activity.ArtNewsDetailActivity;
import artid.com.arttogo.activity.BaseActivity;
import artid.com.arttogo.activity.CollectionDetailPagerActivity;
import artid.com.arttogo.activity.LotDetailPagerActivity;
import artid.com.arttogo.adapter.ArtNewsAdapter;
import artid.com.arttogo.adapter.LotListAdapter;
import artid.com.arttogo.adapter.PersonCollectionAdapter;
import artid.com.arttogo.bean.AppItem;
import artid.com.arttogo.bean.CollectionFragmentNavigationBean;
import artid.com.arttogo.bean.CollectionList;
import artid.com.arttogo.bean.LotSearchDet;
import artid.com.arttogo.bean.NewsList;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.view.pulllist.PullToRefreshLayout;
import artid.com.arttogo.view.pulllist.PullableListView;

/**
 * 项目名称：ArtworkArt
 * 类名称：CollectionFragment
 * 类描述：我的收藏
 * 创建人：耿卫斌
 * 创建时间：2017-3-30 下午4:17:46
 */
public class CollectionFragment extends BaseActivity implements OnClickListener {
    /**
     * 导航栏
     */
    private HorizontalScrollView mHorizontalScrollView;
    /**
     * 导航条目
     */
    private LinearLayout mLinearLayout;
    /**
     * 导航底部线条
     */
    private ImageView mImageView;
    /**
     * 页面展示ViewPager
     */
    private ViewPager pager;
    /**
     * 屏幕宽度
     */
    private int mScreenWidth;
    /**
     * 导航条目宽度
     */
    private int item_width;
    private int endPosition = 0;
    private int beginPosition, currentFragmentIndex;
    private PullableListView mListView;
    private PullToRefreshLayout mPullLayout;
    private boolean isRefreshed = false;// 是否刷新
    private int page = 1;
    private PersonCollectionAdapter mAdapter_Artwork;//艺术品适配器
    private ArtNewsAdapter mAdapter_Information;//资讯适配器
    private LinearLayout noDataLL;
    private List<CollectionFragmentNavigationBean> list_navigation = new ArrayList<CollectionFragmentNavigationBean>();
    private String navigationID;//导航ID（也是请求ID）
    private PersonCollectionAdapter mAdapter_Collection;//藏品适配器
    private LotListAdapter mAdapter_LotList;//拍品适配器
    private ArtNewsAdapter mAdapter_ArtNews;//资讯适配器
    private List<CollectionList> list_Collection = new ArrayList<CollectionList>();//藏品
    private List<CollectionList> moreList_Collection = new ArrayList<CollectionList>();//最终的藏品列表数据
    private List<LotSearchDet> list_LotSearchDet = new ArrayList<LotSearchDet>();//拍品
    private List<LotSearchDet> moreList_LotSearchDet = new ArrayList<LotSearchDet>();//最终的拍品列表数据
    private ArrayList<NewsList> list_NewsList = new ArrayList<NewsList>();//资讯
    private ArrayList<NewsList> moreList_NewsList = new ArrayList<NewsList>();//最终的资讯列表数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_collection);
        initView();
    }

    private void initView() {
        mContext = CollectionFragment.this;
        mBack = (ImageView) findViewById(R.id.back_btn);
        mBack.setOnClickListener(this);
        mTittle = (TextView) findViewById(R.id.tittle_text);
        mTittle.setText("我的收藏");
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.CollectionFragment_hsv_view);
        mLinearLayout = (LinearLayout) findViewById(R.id.CollectionFragment_hsv_content);
        mImageView = (ImageView) findViewById(R.id.CollectionFragment_img1);
        item_width = (int) ((mScreenWidth / 5.0));
        mImageView.getLayoutParams().width = item_width;
        initNav();
        noDataLL = (LinearLayout) findViewById(R.id.ll_hint);
        mListView = (PullableListView) findViewById(R.id.CollectionFragment_ListView);
        mPullLayout = (PullToRefreshLayout) findViewById(R.id.CollectionFragment_refresh_view);
        mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                // TODO Auto-generated method stub
                // 下拉刷新
                page = 1;
                isRefreshed = true;
                getData();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                // TODO Auto-generated method stub
                // 上拉加载
                page++;
                getData();
            }
        });
        getData();
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;

            default:
                break;
        }
    }

    /**
     * 资讯item点击事件
     */
    OnItemClickListener onArtNewsItemClick = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(mContext, ArtNewsDetailActivity.class);
            intent.putExtra("id", moreList_NewsList.get(position).getId());
            startActivity(intent);
        }
    };

    /**
     * 藏品item点击事件
     */
    OnItemClickListener onCollectionItemClick = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(mContext, CollectionDetailPagerActivity.class);
            intent.putExtra("id", moreList_Collection.get(position).getArtid());
            intent.putExtra("titleName", moreList_Collection.get(position).getArtname());
            startActivity(intent);
        }
    };

    /**
     * 拍品item点击事件
     */
    OnItemClickListener onLotListItemClick = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(mContext, LotDetailPagerActivity.class);
            intent.putExtra("id", moreList_LotSearchDet.get(position).getId());
            startActivity(intent);
        }
    };

    /**
     * 设置导航栏
     */
    private void initNav() {
        CollectionFragmentNavigationBean bean2 = new CollectionFragmentNavigationBean();
        bean2.setName("拍品");
        bean2.setId("3");
        list_navigation.add(bean2);
        CollectionFragmentNavigationBean bean = new CollectionFragmentNavigationBean();
        bean.setName("艺术品");
        bean.setId("2");
        list_navigation.add(bean);
        CollectionFragmentNavigationBean bean3 = new CollectionFragmentNavigationBean();
        bean3.setName("资讯");
        bean3.setId("1");
        list_navigation.add(bean3);
        mLinearLayout.removeAllViews();
        navigationID = list_navigation.get(0).getId();
        for (int i = 0; i < list_navigation.size(); i++) {
            RelativeLayout layout = new RelativeLayout(this);
            TextView view = new TextView(this);
            view.setText(list_navigation.get(i).getName());
            view.setGravity(Gravity.CENTER_VERTICAL);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            layout.addView(view, params);
            mLinearLayout.addView(layout, item_width, Cache.dip2px(mContext, 40));
            layout.setOnClickListener(onClickType());
            layout.setTag(i);
        }
    }

    /**
     * 导航标签点击事件
     *
     * @return
     */
    View.OnClickListener onClickType() {
        View.OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                int position = (Integer) arg0.getTag();
                //获取请求标签ID
                navigationID = list_navigation.get(position).getId();
                Animation animation = new TranslateAnimation(endPosition, position * item_width, 0, 0);
                beginPosition = position * item_width;
                currentFragmentIndex = position;
                if (animation != null) {
                    animation.setFillAfter(true);
                    animation.setDuration(0);
                    mImageView.startAnimation(animation);
                    mHorizontalScrollView.smoothScrollTo((currentFragmentIndex - 1) * item_width, 0);
                }
                //选择完导航标签后请求数据
                page = 1;
                isRefreshed = true;
                mAdapter_ArtNews = null;
                mAdapter_Collection = null;
                mAdapter_LotList = null;
                getData();
            }
        };
        return listener;
    }

    /**
     * 获取收藏列表
     * 类型	type	1.资讯2.藏品3.拍品
     * 页码	page
     * 页数	pagesize
     */
    private void getData() {
        //判断解析不同的实体
        Class<? extends AppItem> itemclass = null;
        if (navigationID.equals("1")) {
            itemclass = NewsList.class;
        } else if (navigationID.equals("2")) {
            itemclass = CollectionList.class;
        } else if (navigationID.equals("3")) {
            itemclass = LotSearchDet.class;
        }
        VolleyBasic.getInstance(this).post(Cache.newCollectionList,
                VolleyMap.getInstance().newCollectionList(page, 10, navigationID),
                itemclass, new VolleyCallback() {

                    @SuppressWarnings({"unchecked", "static-access"})
                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
                        if (navigationID.equals("1")) {
                            //资讯数据添加
                            list_NewsList = (ArrayList<NewsList>) vd.getList();
                            if (isRefreshed == true) {
                                moreList_NewsList.clear();
                                isRefreshed = false;
                            }
                            if (!list_NewsList.isEmpty()) {
                                moreList_NewsList.addAll(list_NewsList);
                            }

                            //这里是显示有数据和无数据的布局
                            if (moreList_NewsList != null && moreList_NewsList.size() != 0) {
                                noDataLL.setVisibility(View.GONE);
                            } else {
                                noDataLL.setVisibility(View.VISIBLE);
                            }

                            if (mAdapter_ArtNews == null) {
                                mAdapter_ArtNews = new ArtNewsAdapter(mContext, moreList_NewsList);
                                mListView.setAdapter(mAdapter_ArtNews);
                                mListView.setOnItemClickListener(onArtNewsItemClick);
                            }
                            mAdapter_ArtNews.notifyDataSetChanged();
                        } else if (navigationID.equals("2")) {
                            //藏品数据添加
                            list_Collection = (List<CollectionList>) vd.getList();
                            if (isRefreshed == true) {
                                moreList_Collection.clear();
                                isRefreshed = false;
                            }
                            if (!list_Collection.isEmpty()) {
                                moreList_Collection.addAll(list_Collection);
                            }

                            //这里是显示有数据和无数据的布局
                            if (moreList_Collection != null && moreList_Collection.size() != 0) {
                                noDataLL.setVisibility(View.GONE);
                            } else {
                                noDataLL.setVisibility(View.VISIBLE);
                            }

                            if (mAdapter_Collection == null) {
                                mAdapter_Collection = new PersonCollectionAdapter(mContext, moreList_Collection);
                                mListView.setAdapter(mAdapter_Collection);
                                mListView.setOnItemClickListener(onCollectionItemClick);
                            }
                            mAdapter_Collection.notifyDataSetChanged();
                        } else if (navigationID.equals("3")) {
                            //拍品数据添加
                            list_LotSearchDet = (List<LotSearchDet>) vd.getList();
                            if (isRefreshed == true) {
                                moreList_LotSearchDet.clear();
                                isRefreshed = false;
                            }
                            if (!list_LotSearchDet.isEmpty()) {
                                moreList_LotSearchDet.addAll(list_LotSearchDet);
                            }

                            //这里是显示有数据和无数据的布局
                            if (moreList_LotSearchDet != null && moreList_LotSearchDet.size() != 0) {
                                noDataLL.setVisibility(View.GONE);
                            } else {
                                noDataLL.setVisibility(View.VISIBLE);
                            }

                            if (mAdapter_LotList == null) {
                                mAdapter_LotList = new LotListAdapter(mContext, moreList_LotSearchDet);
                                mListView.setAdapter(mAdapter_LotList);
                                mListView.setOnItemClickListener(onLotListItemClick);
                            }
                            mAdapter_LotList.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub
                        mPullLayout.loadmoreFinish(mPullLayout.FAIL);
                    }
                });
    }
}
